/** 
 * Project Name:sys-portal-Extjs 
 * File Name:MainCtrl.js 
 * Package Name: 
 * Author : 吴Yuan|小森森
 * Date:2018年4月23日下午8:40:20 
 * Copyright (c) 2018, 15909910367@163.com All Rights Reserved. 
 * 
 */
Ext.define('Jason.view.protal.module.systemManager.right.roles.MainCtrl', {
    extend: 'Wys.basic.BaseCtrl',
    alias: 'controller.right_role_MainViewCtrl',
    formItems: function () {
        return [{
            xtype: 'fieldset',
            title: '基本信息',
            itemId: 'rightBasicForm',
            collapsible: false,
            defaultType: 'textfield',
            defaults: {
                width: '100%',
                labelWidth: 80
            },
            items: [
                { fieldLabel: 'id', name: 'id', xtype: 'textfield', flex: 1, hidden: true, allowBlank: true },
                { fieldLabel: '角色名称', name: 'rolename', xtype: 'textfield', allowBlank: false, afterLabelTextTpl: cfg.required },
                {
                    fieldLabel: '数据权限', xtype: 'radiogroup', columns: 3,
                    vertical: true,
                    items: [
                        { boxLabel: '全部', name: 'dataScopeType', inputValue: 'ALL', checked: true },
                        { boxLabel: '本级', name: 'dataScopeType', inputValue: 'THIS_LEVEL' },
                        { boxLabel: '本级以及子级', name: 'dataScopeType', inputValue: 'THIS_LEVEL_CHILDREN' },
                        { boxLabel: '自己', name: 'dataScopeType', inputValue: 'SELF' }
                    ]
                },
                { fieldLabel: '备注', name: 'remarks', xtype: 'textareafield', allowBlank: true }
            ]
        }]
    },
    onNewButtonClick: function (btn) {
        var me = this;
        Ext.create('Wys.form.FormWindow', {
            width: 500,
            title: '角色管理',
            config: {
                operatetype: 'new',
                model: 'Jason.model.protal.module.systemManager.right.roles.RoleModel',
                gridPanel: btn.up('#rolesGrid'),
                items: me.formItems()
            }
        }).show();
    },
    onEditButtonClick: function (grid, record) {
        var me = this;
        if (grid.xtype.indexOf('button') != -1) {
            records = grid.up('#rolesGrid').getSelection();
            if (records.length === 0 || records.length > 1) {
                EUtil.toastWarn('请选择要编辑的数据。<br /><span style="color:red">注：一次只能选择一条数据进行编辑</span>');
                return;
            } else {
                record = records[0];
            }
        }
        Ext.create('Wys.form.FormWindow', {
            width: 500,
            title: ' 『' + record.get('rolename') + '』角色 信息',
            config: {
                model: 'Jason.model.protal.module.systemManager.right.roles.RoleModel',
                operatetype: 'edit',
                gridPanel: me.getView().down('#rolesGrid'),
                items: me.formItems(),
                formData: record
            }
        }).show();
    },
    onDeleteButtonClick: function (btn) {
        var grid = this.getView().down('#rolesGrid'),
            gridStore = grid.getStore(),
            selectGridData = grid.getSelection(),
            removePK = [],
            removeName = [];
        if (selectGridData.length === 0) {
            EUtil.showWarning('请选择要操作的数据');
            return;
        }
        Ext.Array.each(selectGridData, function (item) {
            removeName.push(item.data.rolename);
            removePK.push(item.data.id);
        });
        EUtil.showMsg({
            option: 3,
            title: '温馨提示',
            message: '您确定删除角色<span style="color:red;">『' + removeName.join() + '』</span>吗？',
            callback: function (btn) {
                if (btn === 'ok') {
                    gridStore.remove(selectGridData);
                    gridStore.sync({
                        callback: function () {
                            EUtil.showSuccess('角色『' + removeName.join() + '』<span style="color:red;">删除成功</span>');
                            gridStore.reload();
                        }
                    });
                }
            }
        });
    },
    onRoleGridItemclick: function (grid, record) {
        var view = grid.up('systemManager_roles_usersView');
        haveRightByRoleView = view.down('#haveRightByRoleViewOne'),
            haveRightByRoleViewStore = haveRightByRoleView.getStore();
        haveRightByUsersView = view.down('#haveRightByUsersView');
        haveRightByUsersViewStore = haveRightByUsersView.getStore();
        haveRightByRoleView.setTitle('『' + record.get('rolename') + '』操作模块');
        haveRightByUsersView.setTitle('『' + record.get('rolename') + '』已关联的用户');
        haveRightByRoleView.setDisabled(false);
        haveRightByUsersView.up().setDisabled(false);
        Ext.apply(haveRightByRoleViewStore.proxy.extraParams, {
            roleCode: record.get('id')
        });
        haveRightByRoleViewStore.load();
        haveRightByRoleView.roleCode = record.get('id');
        haveRightByRoleView.rolename = record.get('rolename');
        haveRightByUsersView.rolename = record.get('rolename');
        haveRightByUsersView.roleCode = record.get('id');
        haveRightByUsersView.load({
            roleCode: record.get('id')
        });
    },
    checkboxSelected: function (node, checked) {
        var self = this;
        self.setChildChecked(node, checked);
        self.setParentChecked(node, checked);
    },
    onLoaded: function (treepanel, records, successful, operation, node, eOpts) {
    },
    saveRoleLimit: function (btn) {
        var treeView = this.getView().down('#haveRightByRoleViewOne');
        rightCode = '';
        nodeChecked = treeView.getChecked();
        if (Ext.isEmpty(treeView.roleCode)) {
            EUtil.showWarning('缺少角色信息');
            return;
        }
        Ext.Array.each(nodeChecked, function (rec) {
            var code = rec.get('id');
            if (code.indexOf('extModel') === 0 || code === 'root') {
            } else {
                rightCode += code + ",";
            }
        });
        if (Ext.isEmpty(rightCode)) {
            EUtil.showWarning('请为角色『' + treeView.rolename + '』选择至少一个可操作内容');
            return;
        }
        EUtil.RS({
            url: reqUrl.sys.role.rightRoleAuthorization,
            target: treeView.getEl(),
            msg: true,
            params: {
                'r_code': treeView.roleCode,
                'ri_code': rightCode
            },
            callback: function (resp) {
                if (resp.success) {
                    EUtil.showSuccess('为角色『' + treeView.rolename + '』授权成功');
                } else {
                    EUtil.showError('为角色『' + treeView.rolename + '』授权失败，请稍后再试');
                }
            }
        });
    },
    /**
     * 保存用户到角色内
     */
    onSaveUserToRole: function (btn) {
        var selectUserView = this.getView().down('#haveRightByUsersView');
        if (Ext.isEmpty(selectUserView.roleCode)) {
            EUtil.toastWarn('缺少角色信息');
            return;
        }
        Ext.create('Ext.window.Window', {
            maximizable: false,
            resizable: true,
            closeAction: 'close',
            bodyPadding: '1px 1px',
            shadowOffset: 30,
            height: Ext.getBody().getHeight() * 0.6,
            modal: true,
            layout: 'fit',
            rolename: selectUserView.rolename,
            roleCode: selectUserView.roleCode,
            parentView: selectUserView,
            iconCls: 'fa fa-random',
            title: '为角色【' + selectUserView.rolename + '】分配用户',
            width: 800,
            items: [{ xtype: 'systemManager_rolesToUser_usersView' }]
        }).show();
    },
    // 选择子节点树
    setChildChecked: function (node, checked) {
        var self = this;
        //		node.expand();
        node.set('checked', checked);
        if (node.hasChildNodes()) {
            node.eachChild(function (child) {
                self.setChildChecked(child, checked);
            });
        }
    },
    // 选择父节点树
    setParentChecked: function (node, checked) {
        var self = this;
        node.set({
            checked: checked
        });
        var parentNode = node.parentNode;
        if (parentNode != null) {
            var flag = false;
            parentNode.eachChild(function (childnode) {
                if (childnode.get('checked')) {
                    flag = true;
                }
            });
            if (checked == false) {
                if (!flag) {
                    self.setParentChecked(parentNode, checked);
                }
            } else {
                if (flag) {
                    self.setParentChecked(parentNode, checked);
                }
            }
        }
    }
});